<?php
/* wwwschool project

Copyright (C) 2009  Phillip Aldridge
Email : info@imi21.com
Web site http://www.imi21.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/


/**
 *  File : editLesson.php
 *  called only via ajax 
 *  
 *  requires POST  
 */  


checkSession();
if ($session->userType() > 3) // admin, head master & teacher
{
  die('Wrong user rights');
  exit;
}

require '../../includes/classes/WS3formview.php';
require '../../includes/classes/WS3course.php';
require '../../includes/classes/WS3lesson.php';

$date=date("Y-m-d H:i:s");
$message="";

$course = new WS3_Course();
$lesson = new WS3_Lesson();


  
// are we inserting a new lesson ?
if ($id=='-1') 
{
  $course_id=$_POST['course_id'];
  $course->setCourseID($course_id);
  
  // get the next position for this lesson in this course
  $position=1;
  $dbPosition=new WS3_Database();
  $q="SELECT MAX(position) AS p FROM lesson2course
  WHERE 1 AND course_id= '".$course->CourseID()."'
  LIMIT 1";

  if ($dbPosition->query($q) && $dbPosition->nextRecord() )
  {
    $position=$dbPosition->f('p')+1;
  }
  
  // insert into table lesson
  $dbInsert = new WS3_Database();
  $q="INSERT INTO `lesson` (`id`,`date_created`,`position` ) VALUES (NULL,'$date','$position')";
  $dbInsert->query($q); 
  $lesson_id=$dbInsert->insertID(); 
  
  // insert into table lesson2course 
  $q="INSERT INTO `lesson2course`
  (`id`,`course_id`,`lesson_id`,`position` )
  VALUES
  (NULL,'".$course->CourseID()."','$lesson_id','$position')"; 
  $dbInsert->query($q);  
  
  $lesson->setLessonID( $lesson_id);
  
} // end inserting new lesson
else 
{ // editing existing lesson
  $lesson->setLessonID( $id );
  $course->setCourseID( $lesson->CourseID() );

} // end edit existing lesson



if ( $lesson->LessonID() )
{ // lesson does exisits, build the form to for editing

  $message='';
  $form = new WS3_FormView('main');
  $form->setTitle( 'Edit Lesson '.$lesson->LessonName().' in '.$course->CourseName() );
  $form->displayResetButton( true );
  $form->setAction( 'update' );
  $form->addHiddenField( 'f', 'Lesson' ); // which function to call
  $form->addHiddenField( 'function2', 'list' ); // which function to call after update
  $form->addHiddenField( 'param1', 'lessons' ); // parameter1 for post
  $form->addHiddenField( 'course_id', $lesson->CourseID() );
  $form->addHiddenField( 'id', $lesson->LessonID()  );
  
  $form->addFieldSet( 'info', 'Information');
  $form->addTextField( 'info', 'Name','name','name',$lesson->LessonName() );
  $form->addTextArea( 'info', 'Description','description','description',$lesson->LessonDescription());

  echo $form->display();
  
  $message="Listing";
}else{
  $message="Error : ID not found";
}
echo "document.getElementById('messages').innerHTML='$message';";

if (W3DEBUG) $debug=new WS3_Debug();

?>